﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Globalization;

namespace GBPVRLibrary.MetadataLibrary.TheTVDBInterface
{
    /***
        <Episode>
            <id>332179</id>
            <Combined_episodenumber>1</Combined_episodenumber>
            <Combined_season>1</Combined_season>
            <DVD_chapter/>
            <DVD_discid/>
            <DVD_episodenumber/>
            <DVD_season/>
            <Director>McG</Director>
            <EpImgFlag/>
            <EpisodeName>Pilot</EpisodeName>
            <EpisodeNumber>1</EpisodeNumber>
            <FirstAired>2007-09-24</FirstAired>
            −
            <GuestStars>
            Mieko Hillman|Kristine Blackport|Jim Pirri|Diana Gitelman|Mel Fair|Lynn A. Henderson|Odessa Rae|Jordan Potter|Tasha Campbell|Dale Dye|Matthew Bomer|Bruno Amato|Nicolas Pajon|Wendy Makkena
            </GuestStars>
            <IMDB_ID/>
            <Language>en</Language>
            −
            <Overview>
            Chuck Bartowski is an average computer geek until files upon files of government secrets are downloaded into his brain. He is soon scouted by the CIA and NSA to act in place of their computer.
            </Overview>
            <ProductionCode/>
            <Rating>6.0</Rating>
            <SeasonNumber>1</SeasonNumber>
            <Writer>Josh Schwartz|Chris Fedak</Writer>
            <absolute_number>1</absolute_number>
            <filename>episodes/80348-332179.jpg</filename>
            <lastupdated>1229668139</lastupdated>
            <seasonid>27985</seasonid>
            <seriesid>80348</seriesid>
        </Episode>
     */

    public class TheTVDBEpisode:TheTVDBItem
    {
        public int EpisodeNumber { get; set; }

        public int Id { get; set; }
        public float? CombinedEpisodeNumber { get; set; }
        public float? CombinedSeason { get; set; }
        public float? DVDEpisodeNumber { get; set; }
        public int? DVDSeason { get; set; }
        public DateTime? FirstAired { get; set; }
        public string[] GuestStars { get; set; }
        public string Language { get; set; }
        public string Overview { get; set; }
        public string ProductionCode { get; set; }
        public double? Rating { get; set; }
        public int SeasonNumber { get; set; }
        public string[] Writer { get; set; }
        public int? AbsoluteNumber { get; set; }
        public string ThumbnailUrl { get; set; }
        public int SeasonId { get; set; }
        public int SeriesId { get; set; }
        public string[] Director { get; set; }
        public TimeSpan? Duration { get; set; }
        public List<string> Genre { get; set; }
        public string SeriesName { get; set; }    


        public VideoMetadata ConvertToVideoMetadata(bool UseAiredOrder)
        {
            return new VideoMetadata()
            {
                Date = FirstAired,
                Description = Overview,
                Director = (from p in Director select new GBPVRLibrary.MetadataLibrary.Person() { FullName = p }).ToList(),
                Writer = (from p in Writer select new GBPVRLibrary.MetadataLibrary.Person() { FullName = p }).ToList(),
                Subtitle = Name,
                Title = SeriesName,
                Genre = Genre,
                Duration = Duration,
                Rating = Rating ?? 0,
                Episode = EpisodeNumber,
                Season = SeasonNumber,
                DVDSeason = DVDSeason,
                DVDEpisode = DVDEpisodeNumber,
                MetadataArt = this.Thumbnail == null ? null : new System.Drawing.Image[] { this.Thumbnail }.ToList(),
                Thumbnail = this.Thumbnail
            };
        }

        public TheTVDBEpisode(XElement Element, string Mirror, bool Complete, string Language)
        {
            Id = int.Parse(Element.Element("id").Value);
            Name = Element.Element("EpisodeName").Value;
            CombinedEpisodeNumber = Element.Element("Combined_episodenumber") == null ? null : float.Parse(Element.Element("Combined_episodenumber").Value) as float?;
            CombinedSeason = Element.Element("Combined_season") == null ? null : float.Parse(Element.Element("Combined_season").Value) as float?;
            SeasonNumber = int.Parse(Element.Element("SeasonNumber").Value);
            DVDEpisodeNumber = String.IsNullOrEmpty(Element.Element("DVD_episodenumber").Value)? null : float.Parse(Element.Element("DVD_episodenumber").Value) as float?;
            DVDSeason = String.IsNullOrEmpty(Element.Element("DVD_season").Value) ? null : int.Parse(Element.Element("DVD_season").Value) as int?;
            if (DVDEpisodeNumber != null && DVDSeason == null)
                DVDSeason = SeasonNumber;
            EpisodeNumber = int.Parse(Element.Element("EpisodeNumber").Value);
            FirstAired = Element.Element("FirstAired") == null || String.IsNullOrEmpty(Element.Element("FirstAired").Value) ? null : DateTime.ParseExact(Element.Element("FirstAired").Value, "yyyy-MM-dd", new CultureInfo("en-us")) as DateTime?;
            GuestStars = Element.Element("GuestStars").Value.Split(new char[]{'|'}, StringSplitOptions.RemoveEmptyEntries);
            Overview = Element.Element("Overview").Value;
            Rating = String.IsNullOrEmpty(Element.Element("Rating").Value) ? null : (double.Parse(Element.Element("Rating").Value) / 10) as double?;
            Writer = Element.Element("Writer").Value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            AbsoluteNumber = String.IsNullOrEmpty(Element.Element("absolute_number").Value) ? null : int.Parse(Element.Element("absolute_number").Value) as int?;
            ThumbnailUrl = String.Format("{0}/{1}", Mirror, Element.Element("filename").Value);
            SeasonId = int.Parse(Element.Element("seasonid").Value);
            SeriesId = int.Parse(Element.Element("seriesid").Value);
            Director = Element.Element("Director").Value.Split(new char[] { '|' }, StringSplitOptions.RemoveEmptyEntries); 
            Language = Element.Element("Language").Value;
            ProductionCode = Element.Element("ProductionCode").Value;
            this.Unit = String.Format("{0}x{1}", this.SeasonNumber, this.EpisodeNumber);
            if (Complete)
            {
                if (Element.Element("filename") != null)
                    this.Thumbnail = WebSiteHelper.DownloadImage(String.Format("{0}/banners/{1}", Mirror, Element.Element("filename").Value));
                TheTVDBSeries series = TheTVDBSeries.LoadSeries(Mirror, SeriesId, Language);
                this.SeriesName = series.Name;                
                Genre = series.Genre;
                if (Rating == null || Rating <= 0)
                    Rating = series.Rating;
                if (Duration == null)
                    this.Duration = series.Duration;
            }
        }
    }
}
